<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-message"
          size="mini"
          @click="under_review"
        >待审核</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-message"
          size="mini"
          :disabled="multiple"
          @click="batchPass"
        >批量通过</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据序号" align="center" prop="dataId" v-if="columns[0].visible"/>
      <el-table-column label="指标名称" align="center" prop="ycInde.indeName" v-if="columns[1].visible"/>
      <el-table-column label="部门id" align="center" prop="ycInde.deptId" v-if="columns[2].visible"/>
      <el-table-column label="组织名称" align="center" prop="ycOrg.orgName" v-if="columns[3].visible"/>
      <el-table-column label="填报部室" align="center" prop="ycInde.deptId" :formatter="formatDeptName" v-if="columns[10].visible"/>
      <el-table-column label="数据值" align="center" prop="dataValue" v-if="columns[4].visible"/>
      <el-table-column label="维度值" align="center" prop="dataDimension" v-if="columns[5].visible"/>

      <el-table-column label="维度" align="center" prop="dimension" v-if="columns[6].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.you_dimension" :value="scope.row.dimension"/>
        </template>
      </el-table-column>

      <el-table-column label="状态值" align="center" prop="status" v-if="columns[7].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_status1" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="数据年份" align="center" prop="dataYear" v-if="columns[8].visible"/>

      <el-table-column label="是否达标" align="center" prop="upToStandard" v-if="columns[9].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.up_to_standard" :value="scope.row.upToStandard"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-success"-->
<!--            @click="textdate(scope.row)"-->
<!--            v-hasPermi="['system:data:edit']"-->
<!--          >测试</el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="batchPass(scope.row)"
            v-hasPermi="['system:data:edit']"
          >通过</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePass(scope.row)"
            v-hasPermi="['system:data:edit']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改油厂数据管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="指标序号" prop="indeId">
          <el-input v-model="form.indeId" placeholder="请输入指标序号" />
        </el-form-item>
        <el-form-item label="组织序号" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入组织序号" />
        </el-form-item>
        <el-form-item label="维度值" prop="dataDimension">
          <el-input v-model="form.dataDimension" placeholder="请输入维度值" />
        </el-form-item>
        <el-form-item label="数据值" prop="dataValue">
          <el-input v-model="form.dataValue" placeholder="请输入数据值" />
        </el-form-item>
        <el-form-item label="维度" prop="dimension">
          <el-input v-model="form.dimension" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="数据年份" prop="dataYear">
          <el-input v-model="form.dataYear" placeholder="请输入数据年份" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData,listDataMore, getData, delData, addData, updateData, updateMoreDatas } from "@/api/system/data";
import {addMeasures, getMeasuresByDataId, updateMoreMeasures} from "@/api/system/measures";
import {getUserProfile} from "@/api/system/user";
import {DeptSelect} from "@/api/system/dept";

export default {
  name: "Data",
  dicts: ['you_dimension', 'data_status1','up_to_standard'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 油厂数据管理表格数据
      dataList: [],
      ycdeptOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        indeId: null,
        orgId: null,
        dataDimension: null,
        dataValue: null,
        dimension: null,
        status: 1,
        dataYear: null,
        deptId:null
      },
      //列信息
      columns:[
        {key:0,label:`数据序号`,visible:false},
        {key:1,label:`指标名称`,visible:true},
        {key:2,label:`部门id`,visible:false},
        {key:3,label:`组织名称`,visible:true},
        {key:4,label:`数据值`,visible:true},
        {key:5,label:`纬度值`,visible:true},
        {key:6,label:`维度`,visible:true},
        {key:7,label:`状态值`,visible:true},
        {key:8,label:`数据年份`,visible:true},
        {key:9,label: `是否达标`,visible: true},
        {key:10,label: `填报部室`,visible: true}
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getYcDeptSelect();
  },
  methods: {
    formatDeptName(row){
      for(let i = 0; i < this.ycdeptOptions.length; i++){
        if(this.ycdeptOptions[i].deptId == row.ycInde.deptId){
          return this.ycdeptOptions[i].deptName
        }
      }
    },
    //获取部门下拉选项
    getYcDeptSelect(){
      DeptSelect().then(response =>{
        var deptOptions = [];
        this.ycdeptOptions = response.data;
        deptOptions = this.ycdeptOptions.splice(0, 3)
        console.log("this.ycdeptOptions",this.ycdeptOptions);
      })
    },
    /** 查询油厂数据管理列表 */
    getList() {

      this.loading = true;
      let deptId;
      getUserProfile().then(response => {
        if (response.roleGroup != "超级管理员") {
          deptId = response.data.deptId;
          this.queryParams.deptId = deptId;
          listDataMore(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }else{
          listDataMore(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 待审核按钮操作 */
    under_review(){
      this.queryParams.pageNum = 1;
      this.queryParams.status = 1;
      this.getList();
    },

    // 表单重置
    reset() {
      this.form = {
        dataId: null,
        indeId: null,
        orgId: null,
        dataDimension: null,
        dataValue: null,
        createTime: null,
        dimension: null,
        status: null,
        dataYear: null,
        upToStandard: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dataId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加油厂数据管理";
    },

    /** 通过按钮操作 */
    handleUpdate(row) {
      const value = row.dataValue;
      const measures = row.ycInde.indeFlag;
      const al = row.ycInde.raiseLower;
      let a = 0;
      let m = 0;

      if(value < measures && al == 0){
        const measuresform = {
          dataId: null,
          indeId: null,
          cause: null,
          improvement: null,
          meaStatus: null,
        }
        measuresform.dataId = row.dataId;
        measuresform.indeId = row.indeId;
        measuresform.meaStatus = 0;
        addMeasures(measuresform);
        a = 1;
        this.reset();
        getMeasuresByDataId(row.dataId).then(response =>{
          m = response.data.meaId;
          this.reset();
          getData(row.dataId).then(response => {
            this.form = response.data;
            this.form.status = 3;
            this.form.upToStandard = a;
            this.form.meaId = m;
            updateData(this.form).then(response => {
              this.$modal.msgSuccess("数据已通过");
              this.getList();
            });
          })
        })
      }else if(value > measures && al == 1){
        const measuresform = {
          dataId: null,
          indeId: null,
          cause: null,
          improvement: null,
          meaStatus: null,
        }
        measuresform.dataId = row.dataId;
        measuresform.indeId = row.indeId;
        measuresform.meaStatus = 0;
        addMeasures(measuresform);
        a = 1;
        this.reset();
        getMeasuresByDataId(row.dataId).then(response =>{
          m = response.data.meaId;
          this.reset();
          getData(row.dataId).then(response => {
            this.form = response.data;
            this.form.status = 3;
            this.form.upToStandard = a;
            this.form.meaId = m;
            updateData(this.form).then(response => {
              this.$modal.msgSuccess("数据已通过");
              this.getList();
            });
          })
        })
      }else{
        this.reset();
        //const dataId = row.dataId || this.ids;
        getData(row.dataId).then(response => {
          this.form = response.data;
          this.form.status = 3;
          this.form.upToStandard = a;
          console.log(this.form.meaId)
          updateData(this.form).then(response => {
            this.$modal.msgSuccess("数据已通过");
            this.getList();
          });
        })
      }
    },
    /** 驳回按钮操作 */
    handlePass(row) {
      this.reset();
      const dataId = row.dataId || this.ids
      getData(dataId).then(response => {
        this.form = response.data;
        this.form.status = 2;
        updateData(this.form).then(response => {
          this.$modal.msgSuccess("驳回成功");
          this.getList();
        });
      })
    },
    /** 批量通过 */
    batchPass(row){
      const dataIds = row.dataId || this.ids;
      this.$modal.confirm('是否确认通过油厂数据编号为"' + dataIds + '"的数据项？').then(function() {
        updateMoreDatas(dataIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("数据已通过");
      }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('system/data/export', {
        ...this.queryParams
      }, `data_${new Date().getTime()}.xlsx`)
    }
  },

  /** 提交按钮 */
  submitForm() {
    this.$refs["form"].validate(valid => {
      if (valid) {
        if (this.form.dataId != null) {
          updateData(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          addData(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      }
    });
  },
};
</script>
